package com.yuwei.home.adapter

import android.media.Image
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ImageView
import android.widget.TextView
import com.yuwei.home.R
import com.yuwei.home.entity.CommentEntityItem
import com.yuwei.home.entity.Reply
import com.yuwei.image.glide.GlideImageLoader

/**
 * 视频评论列表适配器
 */
class VideoCommentListAdapter constructor(private val list: MutableList<CommentEntityItem>):BaseExpandableListAdapter() {
    override fun getGroupCount(): Int = list.size

    override fun getChildrenCount(groupPosition: Int): Int = list[groupPosition].replyList.size

    override fun getGroup(groupPosition: Int):CommentEntityItem = list[groupPosition]

    override fun getChild(groupPosition: Int, childPosition: Int): Reply = list[groupPosition].replyList[childPosition]

    override fun getGroupId(groupPosition: Int): Long = list[groupPosition].id.toLong()

    override fun getChildId(groupPosition: Int, childPosition: Int): Long = list[groupPosition].replyList[childPosition].id.toLong()

    override fun hasStableIds(): Boolean = true
    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = true

    /**
     * 一级评论
     */
    override fun getGroupView(groupPosition: Int, childPosition: Boolean, converView: View?, parent: ViewGroup?): View {
        var groupEntity = list[groupPosition]
        val holder:GroupViewHolder
        val view:View
        if (converView == null){
            view = LayoutInflater.from(parent?.context).inflate(R.layout.item_comment_lever_one,parent,false)
            holder = GroupViewHolder(view)
            view.tag = holder
        }else{
            view = converView
            holder = (converView.tag as GroupViewHolder)
        }
        if (groupEntity.userlogo.isEmpty()){
            GlideImageLoader.loadResImage(view.context,R.drawable.comment_icon,holder.tvImage )
        }else{
            GlideImageLoader.loadCircleImage(view.context,holder.tvImage, groupEntity.userlogo)
        }

        //设置文本信息
        holder.apply {
            holder.tvName.text = if (groupEntity.nickname.isNullOrEmpty()) "小财神" else groupEntity.nickname
            holder.tvContent.text = if (groupEntity.nickname.isNullOrEmpty()) "上点击发送酷酷酷" else groupEntity.content
            var tvExpand:TextView = view.findViewById(R.id.tv_expand1)
            if (groupEntity.replyList.isNotEmpty()){
                tvExpand.visibility = View.VISIBLE
                tvExpand.text = if (childPosition) "----收起回复----" else "----展开回复----"
            }else{
                tvExpand.visibility = View.GONE
            }
        }
        return  view

    }
    inner class  GroupViewHolder(view: View){
        var tvName:TextView = view.findViewById(R.id.comment_name)
        var tvContent:TextView = view.findViewById(R.id.comment_content)
        var tvImage:ImageView = view.findViewById(R.id.commit_iv)

    }

    /**
     * 二级评论
     */
    override fun getChildView(groupPosition: Int, childPasition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View {
        var groupEntity = list[groupPosition].replyList[childPasition]
        val holder:GroupViewHolder
        val view:View
        if (convertView == null){
            view = LayoutInflater.from(parent?.context).inflate(R.layout.item_comment_lever_two,parent,false)
            holder = GroupViewHolder(view)
            view.tag = holder
        }else{
            view = convertView
            holder = (convertView.tag as GroupViewHolder)
        }
        if (groupEntity.userlogo.isEmpty()){
            GlideImageLoader.loadResImage(view.context,R.drawable.comment_icon,holder.tvImage )
        }else{
            GlideImageLoader.loadCircleImage(view.context,holder.tvImage, groupEntity.userlogo)
        }
        //设置文本信息
        holder.apply {
            holder.tvName.text = if (groupEntity.nickname.isNullOrEmpty()) "小财神" else groupEntity.nickname
            holder.tvContent.text = if (groupEntity.nickname.isNullOrEmpty()) "上点击发送酷酷酷" else groupEntity.content
        }
        return  view
    }


}